﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Data
{
    public class DealerDao : IDealerDao
    {
        public int GetDealerCountByFilter(string name, int dealerGroupId)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                IEnumerable<Dealer> query = context.Dealers;

                //Filters
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(p => p.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                if (dealerGroupId > 0)
                {
                    query = query.Where(p => p.DealerGroupId == dealerGroupId);
                }

                return query.Count();
            }
        }

        public List<Dealer> GetDealersByFilter(string name, int dealerGroupId, int startIndex, int count, string sorting)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                IEnumerable<Dealer> query = context.Dealers;

                //Filters
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(c => c.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                if (dealerGroupId > 0)
                {
                    query = query.Where(c => c.DealerGroupId == dealerGroupId);
                }

                //Sorting
                //This ugly code is used just for demonstration.
                //Normally, Incoming sorting text can be directly appended to an SQL query.
                if (string.IsNullOrEmpty(sorting) || sorting.Equals("Name ASC"))
                {
                    query = query.OrderBy(c => c.Name);
                }
                else if (sorting.Equals("Name DESC"))
                {
                    query = query.OrderByDescending(c => c.Name);
                }
                else if (sorting.Equals("Code ASC"))
                {
                    query = query.OrderBy(c => c.Code);
                }
                else if (sorting.Equals("Code DESC"))
                {
                    query = query.OrderByDescending(c => c.Code);
                }
                else if (sorting.Equals("DealerGroupId ASC"))
                {
                    query = query.OrderBy(c => c.DealerGroupId);
                }
                else if (sorting.Equals("DealerGroupId DESC"))
                {
                    query = query.OrderByDescending(c => c.DealerGroupId);
                }
                else
                {
                    query = query.OrderBy(c => c.Name); //Default!
                }

                return count > 0
                           ? query.Skip(startIndex).Take(count).ToList() //Paging
                           : query.ToList(); //No paging
            }
        }

        public List<Dealer> GetDealers()
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                return context.Dealers.ToList();
            }
        }

        public Dealer AddDealer(Dealer dealer)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                context.Dealers.AddObject(dealer);
                context.SaveChanges();
            }

            return dealer;
        }

        public void UpdateDealer(Dealer dealer)
        {
            if (dealer == null || dealer.Id == 0) return;

            using (var context = new MonkeyWarrantyEntities())
            {
                context.Dealers.Attach(dealer);
                context.ObjectStateManager.ChangeObjectState(dealer, EntityState.Modified);
                context.SaveChanges();
            }
        }

        public void DeleteDealer(int dealerId)
        {
            if (dealerId <= 0) return;
            using (var context = new MonkeyWarrantyEntities())
            {
                var dealer = context.Dealers.First(c => c.Id == dealerId);

                context.DeleteObject(dealer);

                context.SaveChanges();
            }
        }

        public int GetMaxId()
        {
            var maxId = 0;

            using (var context = new MonkeyWarrantyEntities())
            {
                var dealerGroup = context.Dealers.OrderByDescending(x => x.Id).FirstOrDefault();
                if (dealerGroup != null) maxId = dealerGroup.Id;
            }

            return maxId;
        }

        public int GetCurrentIdentity()
        {
            long? identity;
            using (var context = new MonkeyWarrantyEntities())
            {
                identity = context.GetIdentity("Dealers").SingleOrDefault();
            }

            return identity.HasValue ? (int)identity.Value : 0;
        }

        public Dealer GetById(int id)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                return context.Dealers.FirstOrDefault(x => x.Id == id);
            }
        }
    }
}
